<?php

class Application_Model_TaskMapper extends Ahs_Model_Mapper
{
    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Tasks';
    }
    
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('task_id = ?' => $id));
        return $this->_toTask($row);
    }
    
    public function fetchByProjectId($projectId)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $sql = $db->select()->from('tasks')->where('task_project_id = ?', $projectId)->order('task_endtime ASC');
        $rows = $db->fetchAll($sql);
        return $this->_toTasks($rows);
    }
    
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll();

        return $this->_toTasks($rows);
    }
    
    public function addTask($content, $starttime, $endtime, $projectId)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $count = $db->insert('tasks', array('task_content' => $content, 'task_starttime' => $starttime, 'task_endtime' => $endtime, 'task_project_id' => $projectId));
        if($count > 0)
        {
            return true;
        }
        
        return false;
    }
    
    public function updateTask($id, $content, $starttime, $endtime)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $dataTask = array(
            'task_content' => $content,
            'task_starttime' => $starttime,
            'task_endtime' => $endtime
            );

        if($db->update('tasks', $dataTask,'task_id = '.$id))
        {
            return true;
        }
        else{
            return false;
        }
    }
    
    public function deleteTask($id)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
      
        if($db->delete('tasks', 'task_id = '.$id))
        {
            return true;
        }
        else{
            return false;
        }
    }
    
    private function _toTask($row = null)
    {
        $task = new Application_Model_Task();

        $task->Id            = $row['task_id'];
        $task->Content       = $row['task_content'];
        $task->Starttime     = $row['task_starttime'];
        $task->Endtime       = $row['task_endtime'];
        $task->ProjectId     = $row['task_project_id'];
        
        
        return $task;
    }
    
    private function _toTasks($rows = null)
    {
        $tasks = array();
        
        foreach($rows as $row)
        {
            $tasks[] = $this->_toTask($row);
        }
        
        return $tasks;
    }
    
    public function save(Application_Model_Task $task)
    {
        $data = array(
            'task_id'               => $task->Id,
            'task_content'          => $task->Content,
            'task_starttime'        => $task->Starttime,
            'task_endtime'          => $task->Endtime,
            'task_project_id'       => $task->ProjectId,
        );
        
        
        if(null===$task->Id){
            $this->getDbTable()->insert($data);
        }
        else
        {
           $this->getDbTable()->update($data, array('task_id = ?' => $task->Id));
        }
        
    }

}

